package com.example.musicdemo.ui.activity

import android.view.inputmethod.EditorInfo
import android.widget.ProgressBar
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction
import com.example.musicdemo.R
import com.example.musicdemo.base.BaseActivity
import com.example.musicdemo.presenter.interf.SearchPresenter
import com.example.musicdemo.ui.fragment.SearchFragment
import kotlinx.android.synthetic.main.activity_search.*

class SearchActivity : BaseActivity() {
    private var mSearchPresenter : SearchPresenter? = null
    private lateinit var mFragmentManager : FragmentManager
    private lateinit var mSearchFragment :Fragment
    private lateinit var mFragmentTransaction : FragmentTransaction
    override fun getLayoutId(): Int {
        return R.layout.activity_search
    }

    override fun initListener() {
        super.initListener()
        ed_search.setOnEditorActionListener {
                _, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH){
                mSearchPresenter?.setSearchThing(ed_search.text.toString())
                mSearchPresenter?.loadData()
                //替换fragment
                changeFragment(mSearchFragment)
            }
            false
        }
    }

    override fun initData() {
        super.initData()
        mFragmentManager = supportFragmentManager
        mFragmentTransaction = mFragmentManager.beginTransaction()
        mSearchFragment = SearchFragment()
        mSearchPresenter = (mSearchFragment as SearchFragment).getSpecialPresenter()


    }

    private fun changeFragment(fragment: Fragment){
        mFragmentTransaction.replace(R.id.rv_search,fragment)
        mFragmentTransaction.commit()
    }
}